// pages/signin/signin.ts
let pw_confirm=''
var db=wx.cloud.database()
Page({
  data: {
    nm:'',
    ac:'',
    pw:''
  },

  onLoad() {
    this.setData({
      nm:'',
      ac:'',
      pw:''
    })
  },
  onShow(){
    this.setData({
      nm:'',
      ac:'',
      pw:''
    })
  },
  getAccountInput: function (e:any) {
    this.setData({
      ac:e.detail.value
    })
  },
  getPasswordtInput: function (e:any) {
    this.setData({
      pw:e.detail.value
    })
  },
  getNameInput: function (e:any) {
    this.setData({
      nm:e.detail.value
    })
  },
  passwordConfirm: function (e:any) {
    pw_confirm=e.detail.value
  },
  accountReapt(){
    let that=this
    db.collection('user').where({
      account:this.data.ac
    }).get({
      success: function(res) {
        if(res.data.length>0) {
          wx.showToast({
            title:'该账户已被注册',
            icon:'none'})
        }
        else
        {
          wx.showToast({
            title:'注册成功',
            icon:'success'})
            setTimeout(function(){
              db.collection('user').add({
                data:{
                  name:that.data.nm,
                  account:that.data.ac,
                  password:that.data.pw
                }
              }),
              wx.navigateTo({
                url:'/pages/login/login'
              })
            },1000)
        }
      }
    })
  },
  signIn(){
    if(this.data.ac==''||this.data.pw==''||this.data.nm=='')
    {
      wx.showToast({
        title:'账号或密码或昵称不能为空',
        icon:'none'})
        return;
    }
    if(this.data.pw!=pw_confirm)
    {
      wx.showToast({
        title:'两次密码输入不一致',
        icon:'none'})
        return;
    }
    this.accountReapt()
  }
})